package com.newland.mesdk.demo.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.widget.Button;
import android.widget.TextView;

import com.newland.mesdk.demo.main.R;
import com.newland.mesdk.demo.device.AudioDevice;
import com.newland.mesdk.demo.device.BlueToothDevice;
import com.newland.mesdk.demo.device.K21Device;
import com.newland.mesdk.demo.device.USBDevice;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.main.MyApplication;
import com.newland.mesdk.demo.moduleinterface.DeviceControllerInterface;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.SharedPreferencesUtil;
import com.newland.mesdk.demo.util.Const.MessageTag;

public class BaseActivity extends Activity {
	private TextView tvOperationMessage;
	public String connectType;
	private BlueToothDevice blueTooth;
	private K21Device k21Device;
	private AudioDevice audioDevice;
	private USBDevice usbDevice;
	private DeviceControllerInterface controller;
	private String newMessage = "", message;
	private FragmentManager fm;
	private FragmentTransaction ft;
	private Button btn_init, btn_delet;
	private SharedPreferences sharedPreferences;

	public TextView getTvOperationMessage() {
		return tvOperationMessage;
	}

	public void setTvOperationMessage(TextView tvOperationMessage) {
		this.tvOperationMessage = tvOperationMessage;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

	}

	public void initView() {
		tvOperationMessage = (TextView) findViewById(R.id.test_info);
		btn_init = (Button) findViewById(R.id.btn_init);
		btn_delet = (Button) findViewById(R.id.btn_delet);
		blueTooth = MainActivity.getBlueTooth();
		k21Device = MainActivity.getK21Device();
		audioDevice = MainActivity.getAudioDevice();
		usbDevice = MainActivity.getUsbDevice();
		sharedPreferences = this.getSharedPreferences("mac", Context.MODE_WORLD_READABLE);

		processingUnLock();

	}

	// 连接设备
	public void connectDevice() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				connectType = SharedPreferencesUtil.getStringParam(getApplication(), Const.CONNECTTYPE);
				tvOperationMessage.append("选择的连接方式：" + connectType + "\r\n");
				if (connectType.equals(Const.ConnectTypeName.BLUETOOTH)) { // 蓝牙连接方式
					try {
						if (blueTooth.isControllerAlive()) {
							blueTooth.connectDevice();
							controller = blueTooth.getController();
						} else {
							tvOperationMessage.append("设备控制器未初始化!" + "\r\n");
						}
					} catch (Exception e) {
						tvOperationMessage.append("设备控制器已经异常断开" + e + "\r\n");
					}

				} else if (connectType.equals(Const.ConnectTypeName.K21)) { // K21连接方式
					try {
						if (k21Device.isControllerAlive()) {
							k21Device.connectDevice();
							controller = k21Device.getController();

						} else {
							tvOperationMessage.append("设备控制器未初始化" + "\r\n");
						}
					} catch (Exception e) {
						tvOperationMessage.append("设备控制器已经异常断开" + e + "\r\n");
					}

				} else if (connectType.equals(Const.ConnectTypeName.AUDIO)) { // 音频连接方式
					try {
						if (audioDevice.isControllerAlive()) {
							audioDevice.connectDevice();
							controller = audioDevice.getController();

						} else {
							tvOperationMessage.append("设备控制器未初始化");
						}
					} catch (Exception e) {
						tvOperationMessage.append("设备控制器已经异常断开" + e + "\r\n");
					}

				} else if (connectType.equals(Const.ConnectTypeName.USB)) { // USB连接方式
					try {
						if (usbDevice.isControllerAlive()) {
							usbDevice.connectDevice();
							controller = usbDevice.getController();

						} else {
							tvOperationMessage.append("设备控制器未初始化");
						}
					} catch (Exception e) {
						tvOperationMessage.append("设备控制器已经异常断开" + e + "\r\n");
					}
				} else {
					tvOperationMessage.append("未选择连接方式！请重新初始化" + "\r\n");
					btn_init.setEnabled(true);
					btn_delet.setEnabled(false);
				}

			}

		});

	}

	// 显示操作返回的信息
	public void showMessage(final String mess, final int messageType) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				switch (messageType) {
				case MessageTag.NORMAL:
					message = "<font color='black'>" + mess + "</font>";
					break;
				case MessageTag.ERROR:
					message = "<font color='red'>" + mess + "</font>";
					break;
				case MessageTag.TIP:
					message = "<font color='green'>" + mess + "</font>";
					break;
				case MessageTag.DATA:
					message = "<font color='blue'>" + mess + "</font>";
					break;
				default:
					break;
				}
				newMessage = message + "<br>" + newMessage;
				tvOperationMessage.setText(Html.fromHtml(newMessage));
			}
		});
	}

	// 清楚提示信息
	public void clearMessage() {
		tvOperationMessage.setText("");
		newMessage = "";
	}

	public DeviceControllerInterface getController() {
		return controller;
	}

	// 菜单切换
	public void switchFragment(final int viewContainer, final Fragment newFragment) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				fm = getFragmentManager();
				ft = fm.beginTransaction();
				System.out.println(newFragment);
				ft.replace(viewContainer, newFragment, "tag");
				System.out.println(fm.findFragmentByTag("tag"));
				ft.commit();
			}
		}).start();

	}

	// 判断设备是否已经初始化
	public Boolean isDeviceInit() {
		if (audioDevice.isControllerAlive() || blueTooth.isControllerAlive() || k21Device.isControllerAlive()
				|| usbDevice.isControllerAlive()) {
			return true;
		} else {
			return false;
		}
	}

	public void processingLock() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				SharedPreferences setting = getSharedPreferences("setting", 0);
				SharedPreferences.Editor editor = setting.edit();
				editor.putBoolean("PBOC_LOCK", true);
				editor.commit();
			}
		});

	}

	public boolean processingisLocked() {
		SharedPreferences setting = getSharedPreferences("setting", 0);
		if (setting.getBoolean("PBOC_LOCK", true)) {
			return true;
		} else {
			return false;
		}
	}

	public void processingUnLock() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				SharedPreferences setting = getSharedPreferences("setting", 0);
				SharedPreferences.Editor editor = setting.edit();
				editor.putBoolean("PBOC_LOCK", false);
				editor.commit();
			}
		});

	}

	public SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}

	public void setSharedPreferences(SharedPreferences sharedPreferences) {
		this.sharedPreferences = sharedPreferences;
	}
	
	/**
	 * 设置成设备等待连接状态
	 * 
	 */
	public void btnStateToWaitingConn() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				btn_init.setEnabled(false);
				btn_delet.setEnabled(true);
			}
		});

	}
	/**
	 * 设置成设备等待初始化状态
	 * 
	 */
	public void btnStateToWaitingInit() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				btn_init.setEnabled(true);
				btn_delet.setEnabled(false);
			}
		});

	}

}
